package com.genesis.yunnanji.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.CallBack;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ViewInject;

import es.dmoral.toasty.Toasty;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/5/31.
 */

public class Ecard extends BaseActivity {
    @ViewInject(R.id.tb_ecardpay_toolbar)
    private Toolbar toolbar;
    @ViewInject(R.id.tv_ecardpay_money)
    private TextView money;
    @ViewInject(R.id.et_ecardpay_et1)
    private EditText cardnum;
    @ViewInject(R.id.et_ecardpay_et2)
    private EditText password;
    @ViewInject(R.id.bt_ecardpay_pay)
    private Button pay;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_ecardpay);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        id = getIntent().getStringExtra("id");
        money.setText(getIntent().getFloatExtra("money", -1) + "");
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void pay() {

        OkHttpUtils
                .post()
                .url(GenesisApiConfig.HOST + GenesisApiConfig.ECARD)
                .addParams("orderid", id)
                .addParams("code", cardnum.getText() + "")
                .addParams("pwd", password.getText() + "")
                .addParams("__apptoken__", genesisUtils.getAppToken())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        Log.e("response", response);
                        CallBack callBack = gson.fromJson(response, CallBack.class);
                        if (callBack.getStatus() == 200) {
                            EventBus.getDefault().post(new String("success"));
                            Toasty.success(context, "支付成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toasty.error(context, callBack.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @Override
    protected void setUpView() {
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pay();
            }
        });
    }
}
